<template>
  <a-card :bordered="false">
    <a-spin :spinning="spinning != 0">
      <!-- 查询区域-->

      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="startCodegen" type="primary"
                  icon="plus">
          {{$t('m.codegen.button.genCode')}}
        </a-button>
        <a-button @click="genSpringbootProject" type="primary"
                  icon="plus">
          {{$t('m.codegen.button.genSpringbootProject')}}
        </a-button>
      </div>

      <!-- table区域-begin -->
      <div>
        <a-table
          ref="table"
          size="middle"
          bordered
          :rowKey="rowKey"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="pagination"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          @change="handleTableChange"
          :customRow="clickThenCheck"
        >
          <template slot="serialNum" slot-scope="text,record,index">
            {{(pagination.current-1) * pagination.pageSize + parseInt(index) + 1}}
          </template>
          <template slot="action" slot-scope="text, record">
            <a @click="handleEdit(record)">
              {{ $t('m.common.button.update') }}
            </a>
            <a-divider type="vertical"/>
            <a v-if="record.processStep=='3'" @click="generatorCode(record)">{{$t('m.codegen.button.genCode')}}</a>
            <a-divider v-if="record.processStep=='3'" type="vertical"/>
            <a @click="handleDel(record)">
              {{$t('m.common.button.delete')}}
            </a>
          </template>

        </a-table>
      </div>
    </a-spin>
    <!-- 表单区域 -->
    <CodegenConfigDialog ref="codegenConfigDialog" @refreshParent="modalFormOk"/>
    <BootProjectFormDialog ref="bootProjectFormDialog"/>
  </a-card>
</template>

<script>

  import {ViewMixin} from '@/mixins/ViewMixin'
  import {deleteAction, getAction} from '@/api/manage'
  import CodegenConfigDialog from './modules/CodegenConfigDialog'
  import { downloadFile,downFile } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";
  import BootProjectFormDialog from './modules/bootProjectConfig/BootProjectFormDialog'

  export default {
    name: "CodegenListList",
    mixins: [ViewMixin],
    components: {
      CodegenConfigDialog,
      BootProjectFormDialog
    },
    data() {
      return {
        dictConfig: {},
        optionConfig: {},
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 50,
            align: "center",
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.codegen.entity.tableName'),
            dataIndex: 'tableName',
            align: 'left',
            width: 120,
          },
          {
            title: this.$t('m.codegen.entity.moduleName'),
            dataIndex: 'moduleName',
            align: 'left',
            width: 100,
          },
          {
            title: this.$t('m.codegen.entity.moduleDesc'),
            dataIndex: 'moduleDesc',
            align: 'left',
            width: 100,
          },
          {
            title: this.$t('m.codegen.entity.templateType'),
            dataIndex: 'templateType',
            align: 'center',
            width: 120,
          },
          {
            title: this.$t('m.codegen.entity.withSubTable'),
            dataIndex: 'withSubTable',
            align: 'center',
            width: 80,
          },
          {
            title: this.$t('m.codegen.entity.mainManangerLayout'),
            dataIndex: 'mainManangerLayout',
            align: 'center',
            width: 80,
          },
          {
            title: this.$t('m.codegen.entity.codeCreationDate'),
            dataIndex: 'codeCreationDate',
            align: 'center',
            width: 120,
          },
          {
            title: this.$t('m.common.label.operate'),
            dataIndex: 'action',
            align: "center",
            width: 160,
            scopedSlots: {customRender: 'action'},
          }
        ],
        url: {
          list: YchConstant.PREFIX_EFORM + "/generator/manage/genTable/searchSysGenTableByPage",
          delete: YchConstant.PREFIX_EFORM + "/generator/manage/genConfig/delete",
          deleteBatch: YchConstant.PREFIX_EFORM + "/generator/manage/genTable/deleteBatch",
          genCode: YchConstant.PREFIX_EFORM + '/generator/codeGeneratorController/genCode',
        },
      }
    },
    methods: {
      startCodegen() {
        this.$refs.codegenConfigDialog.add()
      },
      handleEdit(record) {
        this.$refs.codegenConfigDialog.edit(record)
      },
      generatorCode(record){
        let sysGenConfig = {}
        sysGenConfig.id = record.sysGenConfigId
        downloadFile(this.url.genCode, record.moduleName + '.zip', sysGenConfig, 'POST').finally(() => {

        })

      },
      genSpringbootProject(){
        this.$refs.bootProjectFormDialog.add();
      },
      handleDel (record) {
        if (!this.url.delete) {
          this.messageUtil.error('请设置url.delete属性!')
          return
        }
        if (!record) {
          this.messageUtil.warning(this.$t('m.common.tip.select_one_warn'));
          return;
        }
        const that = this
        this.$confirm({
          title: this.$t('m.common.title.delete'),
          content: this.$t('m.common.tip.delete_confirm'),
          onOk: function () {
            that.spinning++
            deleteAction(that.url.delete, { id: record.sysGenConfigId }).then((res) => {
              if (res.success) {
                that.messageUtil.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.messageUtil.warning(res.message)
              }
            }).finally(() => {
              that.spinning--
            })
          }
        })
      },

    },
    watch: {

    }
  }
</script>
<style lang="less" scoped>
</style>
